package com.taoyige.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.taoyige.AppConfig;
import com.taoyige.AppContext;
import com.taoyige.AppException;
import com.taoyige.R;
import com.taoyige.bean.User;
import com.taoyige.common.UIHelper;
import com.taoyige.widget.LoadingDialog;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.SendAuth;
import com.tencent.mm.sdk.openapi.SendAuth.Resp;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
	
    private IWXAPI api;
	private LoadingDialog loading;
	private Handler handler;
	private AppContext appContext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_signin_main);
        appContext=(AppContext) getApplication();
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
    	api = WXAPIFactory.createWXAPI(this, AppConfig.APP_KEY_WECHAT, false);
        
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	// 微信发送请求到第三方应用时，会回调到该方法
	@Override
	public void onReq(BaseReq req) {
		
		switch (req.getType()) {
		
		}
	}

	// 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
	@Override
	public void onResp(BaseResp resp) {
		
		if(resp instanceof SendMessageToWX.Resp){
			final SendMessageToWX.Resp sp=(SendMessageToWX.Resp) resp;
			if(sp!=null){
				switch (sp.errCode) {
				case BaseResp.ErrCode.ERR_OK:
					UIHelper.ToastMessage(WXEntryActivity.this, R.string.share_success);
					finish();
					break;
				case BaseResp.ErrCode.ERR_USER_CANCEL:
					UIHelper.ToastMessage(WXEntryActivity.this, R.string.share_failed);
					finish();
					break;
				case BaseResp.ErrCode.ERR_AUTH_DENIED:
					UIHelper.ToastMessage(WXEntryActivity.this, R.string.share_failed);
					finish();
					break;
			}
		}
		}
		
		if(resp instanceof SendAuth.Resp){
			final SendAuth.Resp sendAuthResp=(Resp) resp;
			if(sendAuthResp!=null){
				switch (resp.errCode) {
				case BaseResp.ErrCode.ERR_OK:
					final String code=sendAuthResp.token;
					
					handler = new Handler() {
						public void handleMessage(Message msg) {
							if (loading != null)
								loading.dismiss();
							if (msg.what == AppConfig.OAUTH_AccessToken_ACCESS&& msg.obj != null) {
							
								User user=(User) msg.obj;
								
								
								UIHelper.showUserCenter(WXEntryActivity.this, 0);
							} else if (msg.obj != null) {
								((AppException) msg.obj).makeToast(WXEntryActivity.this);
							}
							
						}
					};
					
					
					loading = new LoadingDialog(this);
					loading.show();
					new Thread() {
						public void run() {
							Message msg = new Message();
							try {
								User user=appContext.wechatLogin(code);
								appContext.saveLoginInfo(user);
								msg.what = AppConfig.OAUTH_AccessToken_ACCESS;
								msg.obj = user;
							} catch (AppException e) {
								e.printStackTrace();
								msg.what = AppConfig.OAUTH_AccessToken_ERROR;
								msg.obj = e;
							}
							handler.sendMessage(msg);
						}
					}.start();
					break;
				case BaseResp.ErrCode.ERR_USER_CANCEL:
					UIHelper.ToastMessage(WXEntryActivity.this, R.string.OAUTH_AccessToken_CANCEL);
					UIHelper.showUserLogin(WXEntryActivity.this, 0);
					break;
				case BaseResp.ErrCode.ERR_AUTH_DENIED:
					UIHelper.ToastMessage(WXEntryActivity.this, R.string.OAUTH_AccessToken_ERROR);
					UIHelper.showUserLogin(WXEntryActivity.this, 0);
					break;
			}
		}

		}
		
	}
		
	}